<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta name="Content-Type" content="text/html; charset=utf-8" />
<title>Class: Ensembl::DBRegistry::Base</title>
<link rel="stylesheet" href="../../css/style.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="../../css/common.css" type="text/css" media="screen" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  relpath = '../..';
  if (relpath != '') relpath += '/';
</script>
<script type="text/javascript" charset="utf-8" src="../../js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/app.js"></script>

  </head>
  <body>
    <script type="text/javascript" charset="utf-8">
      if (window.top.frames.main) document.body.className = 'frames';
    </script>
    
    <div id="header">
      <div id="menu">
  
    <a href="../../_index.html">Index (B)</a> &raquo; 
    <span class='title'><span class='object_link'><a href="../../Ensembl.html" title="Ensembl (module)">Ensembl</a></span></span> &raquo; <span class='title'><span class='object_link'><a href="../DBRegistry.html" title="Ensembl::DBRegistry (module)">DBRegistry</a></span></span>
     &raquo; 
    <span class="title">Base</span>
  
  
  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  <a id="class_list_link" href="#">Class List</a>
  <a id="method_list_link" href="#">Method List</a>
  <a id ="file_list_link" href="#">File List</a>
</div>

      <div class="clear"></div>
    </div>
    
    <iframe id="search_frame"></iframe>
    
    <div id="content"><h1>Class: Ensembl::DBRegistry::Base
  
  
  
</h1>

<dl class="box">
  
    <dt class="r1">Inherits:</dt>
    <dd class="r1">
      <span class="inheritName">ActiveRecord::Base</span>
      
        <ul class="fullTree">
          <li>Object</li>
          
            <li class="next">ActiveRecord::Base</li>
          
            <li class="next">Ensembl::DBRegistry::Base</li>
          
        </ul>
        <a href="#" class="inheritanceTree">show all</a>
      
      </dd>
    
  
  
    
  
    
  
  
  
    <dt class="r2 last">Defined in:</dt>
    <dd class="r2 last">lib/ensembl/db_connection.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    <p>
The Ensembl::Registry::Base is a super class providing general methods to
get database and connection info.
</p>


  </div>
</div>
<div class="tags">
  
</div><div id="subclasses">
  <h2>Direct Known Subclasses</h2>
  <p class="children"><span class='object_link'><a href="../Core/DBConnection.html" title="Ensembl::Core::DBConnection (class)">Core::DBConnection</a></span>, <span class='object_link'><a href="../Variation/DBConnection.html" title="Ensembl::Variation::DBConnection (class)">Variation::DBConnection</a></span></p>
</div>


  

  
    <h2>
      Class Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#generic_connect-class_method" title="generic_connect (class method)">+ (Object) <strong>generic_connect</strong>(db_type, species, release, args = {}) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'></div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#get_info-class_method" title="get_info (class method)">+ (Object) <strong>get_info</strong> </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'></div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#get_name_from_db-class_method" title="get_name_from_db (class method)">+ (Object) <strong>get_name_from_db</strong>(db_type, species, release, args) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>
Method to retrieve the name of a database, using species, release and
connection parameters passed by the user.
</p>
</div></span>
  
</li>

      
    </ul>
  


  

  <div id="class_method_details" class="method_details_list">
    <h2>Class Method Details</h2>
    
    
      <div class="method_details first">
  <p class="signature first" id="generic_connect-class_method">
  
    + (<tt>Object</tt>) <strong>generic_connect</strong>(db_type, species, release, args = {}) 
  

  
</p><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/ensembl/db_connection.rb', line 86</span>

<span class='def def kw'>def</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='generic_connect identifier id'>generic_connect</span><span class='lparen token'>(</span><span class='db_type identifier id'>db_type</span><span class='comma token'>,</span> <span class='species identifier id'>species</span><span class='comma token'>,</span> <span class='release identifier id'>release</span><span class='comma token'>,</span> <span class='args identifier id'>args</span> <span class='assign token'>=</span> <span class='lbrace token'>{</span><span class='rbrace token'>}</span><span class='rparen token'>)</span>
  <span class='Ensembl constant id'>Ensembl</span><span class='colon2 op'>::</span><span class='SESSION constant id'>SESSION</span><span class='dot token'>.</span><span class='reset identifier id'>reset</span>
  <span class='db_name identifier id'>db_name</span> <span class='assign token'>=</span> <span class='nil nil kw'>nil</span>
  <span class='comment val'># if the connection is established with Ensembl Genomes, set the default port and host</span>
  <span class='if if kw'>if</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:ensembl_genomes</span><span class='rbrack token'>]</span> <span class='then then kw'>then</span>
    <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:port</span><span class='rbrack token'>]</span> <span class='assign token'>=</span> <span class='EG_PORT constant id'>EG_PORT</span>
    <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:host</span><span class='rbrack token'>]</span> <span class='assign token'>=</span> <span class='EG_HOST constant id'>EG_HOST</span>
  <span class='end end kw'>end</span>    
  <span class='if if kw'>if</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:port</span><span class='rbrack token'>]</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span> <span class='then then kw'>then</span>
    <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:port</span><span class='rbrack token'>]</span> <span class='assign token'>=</span> <span class='lparen token'>(</span> <span class='release identifier id'>release</span> <span class='gt op'>&gt;</span> <span class='integer val'>47</span> <span class='rparen token'>)</span> <span class='question op'>?</span> <span class='integer val'>5306</span> <span class='colon op'>:</span> <span class='integer val'>3306</span>
  <span class='end end kw'>end</span>
  <span class='if if kw'>if</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:database</span><span class='rbrack token'>]</span>
    <span class='db_name identifier id'>db_name</span> <span class='assign token'>=</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:database</span><span class='rbrack token'>]</span>
  <span class='else else kw'>else</span> 
    <span class='db_name identifier id'>db_name</span> <span class='assign token'>=</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='get_name_from_db identifier id'>get_name_from_db</span><span class='lparen token'>(</span><span class='db_type identifier id'>db_type</span><span class='comma token'>,</span><span class='species identifier id'>species</span><span class='comma token'>,</span><span class='release identifier id'>release</span><span class='comma token'>,</span><span class='args identifier id'>args</span><span class='rparen token'>)</span> <span class='comment val'># try to find the corresponding database </span>
  <span class='end end kw'>end</span> 
  <span class='establish_connection identifier id'>establish_connection</span><span class='lparen token'>(</span>
                      <span class='symbol val'>:adapter</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:adapter</span><span class='rbrack token'>]</span> <span class='orop op'>||</span> <span class='Ensembl constant id'>Ensembl</span><span class='colon2 op'>::</span><span class='DB_ADAPTER constant id'>DB_ADAPTER</span><span class='comma token'>,</span>
                      <span class='symbol val'>:host</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:host</span><span class='rbrack token'>]</span> <span class='orop op'>||</span> <span class='Ensembl constant id'>Ensembl</span><span class='colon2 op'>::</span><span class='DB_HOST constant id'>DB_HOST</span><span class='comma token'>,</span>
                      <span class='symbol val'>:database</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='db_name identifier id'>db_name</span><span class='comma token'>,</span>
                      <span class='symbol val'>:username</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:username</span><span class='rbrack token'>]</span> <span class='orop op'>||</span> <span class='Ensembl constant id'>Ensembl</span><span class='colon2 op'>::</span><span class='DB_USERNAME constant id'>DB_USERNAME</span><span class='comma token'>,</span>
                      <span class='symbol val'>:password</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:password</span><span class='rbrack token'>]</span> <span class='orop op'>||</span> <span class='Ensembl constant id'>Ensembl</span><span class='colon2 op'>::</span><span class='DB_PASSWORD constant id'>DB_PASSWORD</span><span class='comma token'>,</span>
                      <span class='symbol val'>:port</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:port</span><span class='rbrack token'>]</span>
                    <span class='rparen token'>)</span>
  
  <span class='self self kw'>self</span><span class='dot token'>.</span><span class='retrieve_connection identifier id'>retrieve_connection</span> <span class='comment val'># Check if the connection is working       </span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="get_info-class_method">
  
    + (<tt>Object</tt>) <strong>get_info</strong> 
  

  
</p><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


44
45
46
47
48
49
50
51
52
53</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/ensembl/db_connection.rb', line 44</span>

<span class='def def kw'>def</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='get_info identifier id'>get_info</span>
  <span class='host identifier id'>host</span><span class='comma token'>,</span><span class='user identifier id'>user</span><span class='comma token'>,</span><span class='password identifier id'>password</span><span class='comma token'>,</span><span class='db_name identifier id'>db_name</span><span class='comma token'>,</span><span class='port identifier id'>port</span> <span class='assign token'>=</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='retrieve_connection identifier id'>retrieve_connection</span><span class='dot token'>.</span><span class='instance_values identifier id'>instance_values</span><span class='lbrack token'>[</span><span class='string val'>&quot;connection_options&quot;</span><span class='rbrack token'>]</span>
  <span class='db_name identifier id'>db_name</span> <span class='match op'>=~</span><span class='regexp val'>/(\w+_\w+)_(core|variation|funcgen|compara)_(\d+)_\S+/</span>
  <span class='species identifier id'>species</span><span class='comma token'>,</span><span class='release identifier id'>release</span> <span class='assign token'>=</span> <span class='$1 nth_ref id'>$1</span><span class='comma token'>,</span><span class='$3 nth_ref id'>$3</span> <span class='comment val'># just works for standard Ensembl database names</span>
  <span class='if if kw'>if</span> <span class='species identifier id'>species</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span> <span class='and and kw'>and</span> <span class='release identifier id'>release</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span> <span class='then then kw'>then</span>
    <span class='raise identifier id'>raise</span> <span class='NameError constant id'>NameError</span><span class='comma token'>,</span> <span class='dstring node'>&quot;Can't get database name from #{db_name}. Are you using non conventional names?&quot;</span>
  <span class='else else kw'>else</span>
    <span class='return return kw'>return</span> <span class='host identifier id'>host</span><span class='comma token'>,</span><span class='user identifier id'>user</span><span class='comma token'>,</span><span class='password identifier id'>password</span><span class='comma token'>,</span><span class='db_name identifier id'>db_name</span><span class='comma token'>,</span><span class='port identifier id'>port</span><span class='comma token'>,</span><span class='species identifier id'>species</span><span class='comma token'>,</span><span class='release identifier id'>release</span><span class='dot token'>.</span><span class='to_i identifier id'>to_i</span>
  <span class='end end kw'>end</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="get_name_from_db-class_method">
  
    + (<tt>Object</tt>) <strong>get_name_from_db</strong>(db_type, species, release, args) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Method to retrieve the name of a database, using species, release and
connection parameters passed by the user.
</p>


  </div>
</div>
<div class="tags">
  
</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/ensembl/db_connection.rb', line 57</span>

<span class='def def kw'>def</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='get_name_from_db identifier id'>get_name_from_db</span><span class='lparen token'>(</span><span class='db_type identifier id'>db_type</span><span class='comma token'>,</span><span class='species identifier id'>species</span><span class='comma token'>,</span><span class='release identifier id'>release</span><span class='comma token'>,</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
  <span class='species identifier id'>species</span> <span class='assign token'>=</span> <span class='species identifier id'>species</span><span class='dot token'>.</span><span class='underscore identifier id'>underscore</span> <span class='comment val'># Always in lowercase. This keeps things simple when dealing with complex species names like in Ensembl Genomes database</span>
  <span class='dummy_db identifier id'>dummy_db</span> <span class='assign token'>=</span> <span class='DummyDBConnection constant id'>DummyDBConnection</span><span class='dot token'>.</span><span class='connect identifier id'>connect</span><span class='lparen token'>(</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
  <span class='dummy_connection identifier id'>dummy_connection</span> <span class='assign token'>=</span> <span class='dummy_db identifier id'>dummy_db</span><span class='dot token'>.</span><span class='connection identifier id'>connection</span>
  <span class='comment val'># check if a database exists with exactly the species name passed (regular way)</span>
  <span class='db_name identifier id'>db_name</span> <span class='assign token'>=</span> <span class='dummy_connection identifier id'>dummy_connection</span><span class='dot token'>.</span><span class='select_values identifier id'>select_values</span><span class='lparen token'>(</span><span class='dstring node'>&quot;SHOW DATABASES LIKE '%#{species}_#{db_type}_#{release.to_s}%'&quot;</span><span class='rparen token'>)</span><span class='lbrack token'>[</span><span class='integer val'>0</span><span class='rbrack token'>]</span>
  <span class='comment val'># if a database is not found and we are working on Ensembl Genomes database...</span>
  <span class='if if kw'>if</span> <span class='db_name identifier id'>db_name</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span> <span class='and and kw'>and</span> <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:ensembl_genomes</span><span class='rbrack token'>]</span> <span class='then then kw'>then</span>
    <span class='words identifier id'>words</span> <span class='assign token'>=</span> <span class='species identifier id'>species</span><span class='dot token'>.</span><span class='split identifier id'>split</span><span class='lparen token'>(</span><span class='regexp val'>/_/</span><span class='rparen token'>)</span>
    <span class='first identifier id'>first</span> <span class='assign token'>=</span> <span class='words identifier id'>words</span><span class='dot token'>.</span><span class='shift identifier id'>shift</span>
    <span class='comment val'># ...try to find a collection database using the first name of the species passed (convention used for collection databases)</span>
    <span class='db_name identifier id'>db_name</span> <span class='assign token'>=</span> <span class='dummy_connection identifier id'>dummy_connection</span><span class='dot token'>.</span><span class='select_values identifier id'>select_values</span><span class='lparen token'>(</span><span class='string val'>&quot;SHOW DATABASES&quot;</span><span class='rparen token'>)</span><span class='dot token'>.</span><span class='select identifier id'>select</span> <span class='lbrace token'>{</span><span class='bitor op'>|</span><span class='d identifier id'>d</span><span class='bitor op'>|</span> <span class='d identifier id'>d</span><span class='match op'>=~</span><span class='dregexp node'>/#{first}.*_collection_#{db_type}_#{release.to_s}/</span><span class='rbrace token'>}</span><span class='lbrack token'>[</span><span class='integer val'>0</span><span class='rbrack token'>]</span>
    <span class='comment val'># if a collection database match is found, then look inside to find the species</span>
    <span class='if if kw'>if</span> <span class='db_name identifier id'>db_name</span> <span class='neq op'>!=</span> <span class='nil nil kw'>nil</span> <span class='then then kw'>then</span>
      <span class='dummy_db identifier id'>dummy_db</span><span class='dot token'>.</span><span class='disconnect! fid id'>disconnect!</span> <span class='comment val'># close the generic connection with the host</span>
      <span class='args identifier id'>args</span><span class='lbrack token'>[</span><span class='symbol val'>:database</span><span class='rbrack token'>]</span> <span class='assign token'>=</span> <span class='db_name identifier id'>db_name</span>
      <span class='dummy_db identifier id'>dummy_db</span> <span class='assign token'>=</span> <span class='DummyDBConnection constant id'>DummyDBConnection</span><span class='dot token'>.</span><span class='connect identifier id'>connect</span><span class='lparen token'>(</span><span class='args identifier id'>args</span><span class='rparen token'>)</span> <span class='comment val'># open a new connection directly with the collection database</span>
      <span class='species_name identifier id'>species_name</span> <span class='assign token'>=</span> <span class='species identifier id'>species</span><span class='dot token'>.</span><span class='gsub identifier id'>gsub</span><span class='lparen token'>(</span><span class='first identifier id'>first</span><span class='comma token'>,</span><span class='first identifier id'>first</span><span class='lbrack token'>[</span><span class='integer val'>0</span><span class='dot2 op'>..</span><span class='integer val'>0</span><span class='rbrack token'>]</span><span class='rparen token'>)</span> <span class='comment val'># transform the species name, so it can match the species names stored in the collection database</span>
      <span class='Ensembl constant id'>Ensembl</span><span class='colon2 op'>::</span><span class='SESSION constant id'>SESSION</span><span class='dot token'>.</span><span class='collection_species identifier id'>collection_species</span> <span class='assign token'>=</span> <span class='species_name identifier id'>species_name</span> <span class='comment val'># set the species used for this session, so it's easier to fetch slices from the genome of that species</span>
      
      <span class='comment val'># check that the species passed is present in the collection database, otherwise returns a warning</span>
      <span class='exists identifier id'>exists</span> <span class='assign token'>=</span> <span class='dummy_db identifier id'>dummy_db</span><span class='dot token'>.</span><span class='connection identifier id'>connection</span><span class='dot token'>.</span><span class='select_values identifier id'>select_values</span><span class='lparen token'>(</span><span class='dstring node'>&quot;SELECT species_id FROM meta WHERE LOWER(meta_value) = '#{species_name}' AND meta_key = 'species.db_name'&quot;</span><span class='rparen token'>)</span><span class='lbrack token'>[</span><span class='integer val'>0</span><span class='rbrack token'>]</span>
      <span class='warn identifier id'>warn</span> <span class='dstring node'>&quot;WARNING: No species '#{species}' found in the database. Please check that the name is correct.&quot;</span> <span class='if if_mod kw'>if</span> <span class='notop op'>!</span><span class='exists identifier id'>exists</span>
    <span class='end end kw'>end</span>
  <span class='end end kw'>end</span>
  <span class='warn identifier id'>warn</span> <span class='dstring node'>&quot;WARNING: No connection to database established. Check that the species is in snake_case (was: #{species}).&quot;</span> <span class='if if_mod kw'>if</span> <span class='db_name identifier id'>db_name</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span>
  <span class='return return kw'>return</span> <span class='db_name identifier id'>db_name</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>
    
    <div id="footer">
  Generated on Wed Dec 15 19:18:19 2010 by 
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.6.3 (ruby-1.8.7).
</div>

  </body>
</html>